package com.graduation.sports.ui.login;

import android.content.Intent;
import android.os.Bundle;
import android.text.TextUtils;
import android.view.View;
import android.widget.Button;
import android.widget.CheckBox;
import android.widget.CompoundButton;
import android.widget.EditText;

import com.blankj.utilcode.util.SPUtils;
import com.blankj.utilcode.util.ToastUtils;
import com.graduation.sports.R;
import com.graduation.sports.base.BaseActivity;
import com.graduation.sports.bean.UserInfo;
import com.graduation.sports.config.Session;
import com.graduation.sports.contract.LoginContract;
import com.graduation.sports.presenter.LoginPresenter;
import com.graduation.sports.ui.activity.MainActivity;
import com.graduation.sports.ui.activity.StartSportActivity;
import com.lxj.xpopup.XPopup;

public class SportsLoginActivity extends BaseActivity implements LoginContract.View {

    private EditText et_username;
    private EditText et_password;
    private Button btnLogin;
    private Button btnRegister;
    private CheckBox isRemember;

    private LoginPresenter loginPresenter;

    @Override
    protected int getLayout() {
        return R.layout.activity_sports_login;
    }

    @Override
    protected void initDataAndEvent(Bundle savedInstanceState) {
        loginPresenter = new LoginPresenter(this);
        initView();
    }

    private void initView(){
        et_username = findView(R.id.login_et_username);
        et_password = findView(R.id.login_et_password);
        btnLogin = findView(R.id.login_bt_login);
        btnRegister = findView(R.id.login_bt_register);
        isRemember = findView(R.id.remember_account);

        if (SPUtils.getInstance().getBoolean("isRemember")) {
            isRemember.setChecked(true);
            et_username.setText(SPUtils.getInstance().getString("account"));
            et_password.setText(SPUtils.getInstance().getString("password"));
        }

        isRemember.setOnCheckedChangeListener(new CompoundButton.OnCheckedChangeListener() {
            @Override
            public void onCheckedChanged(CompoundButton buttonView, boolean isChecked) {
                SPUtils.getInstance().put("isRemember", isChecked);
            }
        });


        btnLogin.setOnClickListener(view -> {
            String username = et_username.getText().toString().trim();
            String password = et_password.getText().toString().trim();

            if (TextUtils.isEmpty(username)) {
                ToastUtils.showShort("清输入用户名");
                return;
            }
            if (TextUtils.isEmpty(password)) {
                ToastUtils.showShort("清输入密码");
                return;
            }
            showLoadingDialog("正在登录···");
            loginPresenter.login(username, password);
        });

        btnRegister.setOnClickListener(new View.OnClickListener() {
            @Override
            public void onClick(View view) {
//                openActivity(RegisterActivity.class);
                startActivity(new Intent(SportsLoginActivity.this, RegisterActivity.class));
            }
        });

    }

    @Override
    public void onLoginSuccess(UserInfo userInfo) {
        closeLoadingDialog();
        showToast("登录成功");
        Session.setUserInfo(userInfo);
        if (isRemember.isChecked()) {
            SPUtils.getInstance().put("account",userInfo.getUserAccount());
            SPUtils.getInstance().put("password",userInfo.getPassword());
            SPUtils.getInstance().put("isRemember",true);
        }
        openActivity(MainActivity.class);
//        startActivity(new Intent(SportsLoginActivity.this, StartSportActivity.class));
        finish();
    }



    @Override
    public void onLoginFailed(String description) {
        closeLoadingDialog();
        showToast(description);
    }


}
